package com.sunwhite.jdbc.test;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class JdbcMain {

  public static void main(String[] args) {
    // JDBC测试
    String url = "jdbc:mysql://119.3.228.251:3306/spring_demo";
    String username = "root";
    String password = "Zhuang.21314";
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection connection = DriverManager.getConnection(url, username, password);
      connection.setAutoCommit(false);
      PreparedStatement preparedStatement = connection.prepareStatement("select * from dept");
      ResultSet resultSet = preparedStatement.executeQuery();
      ArrayList<Map<String, Object>> list = new ArrayList<>();
      Map<String, Object> map = null;
      while (resultSet.next()) {
        map = new HashMap<>();
        Integer rowCount = resultSet.getMetaData().getColumnCount();
        for (Integer i = 1; i <= rowCount; i++) {
          ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
          String rowName = resultSetMetaData.getColumnName(i);
          map.put(rowName, resultSet.getString(rowName));
        }
        list.add(map);
      }
      list.forEach(
          item -> {
            for (Map.Entry<String, Object> entry : item.entrySet()) {
              //
              System.out.println(entry.getValue());
            }
          });
      resultSet.close();
      preparedStatement.close();
      connection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
